
public class CMalloc extends CPointer{
 private static native long malloc(int size) ;
 public CMalloc(int size）throws OutOfMemoryError{
  peer = Malloc(size) ;
  if (peer == 0){
   throw new OutOfMemoryError();
  }
 }
 public native void free() ;
 ...
}
public abstract class CPointer{
	 protected long peer;
	 public native void copyIn(
	   int bOff,  // offset from a C pointer
	   int[] buf,  // source data
	   int off,  // offset into source
	   int len);  // number of elements to be copied
	 public native void copyOut(...) ;
	 ...
}


pubic class CFunction extends CPointer{
	private static final int CONV_C = 0 ;
	private static final int CONV_JNI = 1 ;
	private int conv ;
	private native long find(String lib, String fname) ;

	public CFunction(String lib,   // native library name
		String fname,  // C function name
		String conv){  // Calling convention
		if( conv.equals("C"））{
			conv = CONV_C ;
		}else if (conv.equals("JNI")){
			conv = CONV_JNI ;
		}else{
			throw new IllegalArgumentException("bad calling convention") ;
		}
		peer = find(lib, fname) ;
	}
	pubic native int callInt(Object[] args) ;
	...
}
public class C{
 private static CFunction c_atol =
  new CFunction("msvcrt.dll",  // native library name
    "atol",   // C function name
    "C") ;   // calling convention
 public static int atol(string str){ 
  return c_atol.callInt(new Object[]{str}) ;
 }
 ...
}





public class Win32{
 private static CFunction c_CreateFile =
  new CFunction("kernel32.dll",  // native library name
    "CreateFileA",  // native function
    "JNI") ;   // calling convention
 
 public static int CreateFile(
  String filename,   // file name
  int desiredAccess,  // access (read-write) mode
  int shareMode,   // share mode
  int[] secAttrs,   // security attributes
  int creationDistribution, // how to create
  int flagAndAttributes,  // file attributes
  int templateFile)   // file with attr. to copy
 {
  CMalloc cSecAttrs = null ;
 
  if( secAttrs ! = NULL ) {
   cSecAttrs = new CMalloc(secAttrs.length*4) ;
   cSecAttrs.copyIn(0, secAttrs, 0, secAttrs.length) ;
  }
  try{
   return c_CreateFile.callInt(new Object[]{
      filename,
      new Integer(desiredAccess),
      new Integer(shareMode),
      cSecAttrs,
      new Integer(creationDistribution),
      new Integer(flagAndAttributes),
      new Integer(templateFile)} ) ;
  }
  finally{
   if( secAttrs != NULL ){
    cSecAttrs.free() ;
   }
  }
 }
 ...
}